package com.oceanread.cn.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oceanread.cn.config.Result;
import com.oceanread.cn.entity.Sediment;
import com.oceanread.cn.entity.User;
import com.oceanread.cn.entity.UserTools;
import com.oceanread.cn.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin()
@Api(value="用户controller",tags={"1-User用户操作接口"})
public class UserController {
    @Autowired
    private IUserService iUserService;

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public Result<?> register(@ApiParam(name = "user", value = "用户信息") @RequestBody User user) {
        System.out.println("用户请求注册：   用户名：" + user.getUsername() + "   密码：" + user.getPassword());
        /*判断输入内容是否正确*/
        if (user.getUsername() == null || user.getUsername() == "") {
            return Result.error("1", "请输入用户名！");
        } else if (user.getPassword() == null || user.getPassword() == "") {
            return Result.error("1", "请输入密码！");
        } else {
            /*输入信息有效*/
            User userSearch = iUserService.login(user.getUsername());
            if (userSearch == null) {
                iUserService.insert(user.getUsername(), user.getPassword());
                return Result.success();
            } else {
                return Result.error("1", "用户名已存在！");
            }
        }
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        System.out.println("用户请求登录：   用户名：" + user.getUsername() + "   密码：" + user.getPassword());
        if (user.getUsername() == null || user.getUsername() == "") {
            return Result.error("1", "请输入用户名！");
        } else if (user.getPassword() == null || user.getPassword() == "") {
            return Result.error("1", "请输入密码！");
        } else {
            User userlogin = iUserService.login(user.getUsername());
            if (userlogin != null) {
                UserTools userTools=new UserTools();
                String a = new String(userlogin.getPassword());
                String b = new String(user.getPassword());
                if (a.equals(b)) {
                    return Result.success(userTools.getinfo(userlogin));
                } else {
                    return Result.error("1", "密码不正确！");
                }
            } else {
                return Result.error("1", "用户不存在");
            }
        }
    }

    @ApiIgnore(value = "忽略显示")
    @PostMapping("/getall")
    public Result getall(@RequestBody User user) {
        System.out.println("验证权限：   用户名：" + user.getUsername() + "   密码：" + user.getPassword());
        Result result = new Result();
        if (user.getUsername() == null || user.getUsername() == "") {
            return Result.error("1", "请输入用户名！");
        } else if (user.getPassword() == null || user.getPassword() == "") {
            return Result.error("1", "请输入密码！");
        } else {
            User userlogin = iUserService.login(user.getUsername());
            if (userlogin != null) {
                String a = new String(userlogin.getPassword());//数据库密码信息
                String b = new String(user.getPassword());//前端信息
                String c = userlogin.getRole();//用户等级
                System.out.println(c);
                if (a.equals(b)) {
                    if (c.equals("1")) {
                        result.setCode("0");
                        result.setMsg("成功");
                        UserTools userTools=new UserTools();
                        JSONArray jsonArray = new JSONArray();
                        for (User user1 : iUserService.getall()) {
                            JSONObject object = new JSONObject();
                            object = userTools.getinfo(user1);
                            jsonArray.add(object);
                        }
                        result.setData(jsonArray);
                        return result;
                    } else {
                        return Result.error("1", "用户等级不够");
                    }
                } else {
                    return Result.error("1", "用户密码错误");
                }
            } else {
                return Result.error("1", "验证用户失败！");
            }
        }
    }

    @ApiOperation(value = "用户列表__分页")
    @GetMapping("/getList")
    public JSONObject getList(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                              @RequestParam(name = "size", required = true, defaultValue = "10") int size) {
        UserTools userTools=new UserTools();
        JSONArray jsonArray = new JSONArray();
        for (User user1 : iUserService.getTablelist(page, size)) {
            JSONObject object = new JSONObject();
            object = userTools.getinfo(user1);
            jsonArray.add(object);
        }
        JSONObject object = new JSONObject();
        object.put("data", jsonArray);
        object.put("total", iUserService.getnum());
        return object;
    }

    @ApiOperation(value = "用户信息更新")
    @PostMapping("/update")
    public Integer update(@RequestBody User user) {
        UserTools userTools =new UserTools();
        System.out.println(userTools.getinfo(user));
        return iUserService.update(user);
    }

    @ApiOperation(value = "用户删除")
    @GetMapping("/delete")
    public Integer delete(@RequestParam(name = "id") int id) {
        return iUserService.delete(id);
    }

}
